package com.ruoyi.web.common.util.format;

import com.ruoyi.common.utils.msg.util.Crc16Util;
import com.ruoyi.iot.domain.IotDataWaterQuality;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class WaterQualityUtil {

    public static String convertToHydrologicalProtocol(IotDataWaterQuality waterQuality) throws DecoderException {
        String protocol = "";
        String deviceNo = "b070209239";
        String header = "7e7e 03 " + deviceNo +" a000 32 0030 02";
        //添加流水号
        String sequence = getNextSequence();
        String preData = header + sequence + getCurrentTimeFormatted(true) + "f1f1" + deviceNo + "51" + "f0f0" + getCurrentTimeFormatted(false);
        String data = preData
            + "4612" + NumberFormatterUtil.formatNumber(waterQuality.getValuePH(),"12")//PH 值 4612XXXX
            + "4711" + NumberFormatterUtil.formatNumber(waterQuality.getValueDO(),"11")//DO 值 4711XXXX
            + "4618" + NumberFormatterUtil.formatNumber(waterQuality.getValueCOND(),"18")//COND 值 4618XXXX
            + "4610" + NumberFormatterUtil.formatNumber(waterQuality.getValueTURB(),"10")//TURB 值 4610XXXX
            + "461a" + NumberFormatterUtil.formatNumber(waterQuality.getValueNH4N(),"1a")//NH4N 值
            + "461b" + NumberFormatterUtil.formatNumber(waterQuality.getValueTP(),"1b")
            + "4612" + NumberFormatterUtil.formatNumber(waterQuality.getValueCHLA(),"12")
            + "4611" + NumberFormatterUtil.formatNumber(waterQuality.getValueC(),"11")
            + "4622" + NumberFormatterUtil.formatNumber(waterQuality.getValueZMD(),"22")
            + "461a" + NumberFormatterUtil.formatNumber(waterQuality.getValueCOD(),"1a")
            ;
        protocol = data + "03";
        String protocolLower = protocol.replace(" ", "").toLowerCase();

        String crcVerifyCode = Crc16Util.crc16(Hex.decodeHex(protocolLower.replaceAll(" ", "")), false);
        protocolLower = protocolLower + crcVerifyCode;
        return protocolLower;
    }

    private static int sequence = 0;

    public static synchronized String getNextSequence() {
        sequence++;
        if (sequence > 65535) {
            sequence = 1;
        }
        return String.format("%04X", sequence);

    }

    /**
     * 获取当前时间并格式化为yyyyMMddHHmmss格式
     *
     * 此方法用于获取当前系统时间，并将其格式化为一个易于理解和使用的字符串格式
     * 选择此格式以便在生成文件名或唯一标识符时确保时间戳的唯一性和排序性
     *
     * @return 当前时间格式化后的字符串
     */
    private static String getCurrentTimeFormatted(Boolean isSecondTime) {
        // 获取当前系统日期和时间
        LocalDateTime now = LocalDateTime.now();
        // 创建一个日期时间格式化器，用于将日期时间格式化为yyMMddHHmmss格式
        if (isSecondTime){
            // 使用格式化器将当前日期时间格式化为字符串并返回
            return now.format(DateTimeFormatter.ofPattern("yyMMddHHmmss"));
        }else {
            return now.format(DateTimeFormatter.ofPattern("yyMMddHHmm"));
        }
    }

    public static void main(String[] args) {
        IotDataWaterQuality waterQuality = new IotDataWaterQuality();
        waterQuality.setValuePH("6.5");
        waterQuality.setValueDO("6.5");
    }

}
